package cn.yanhu.kuwanapp.earn.fragment

import android.content.Context
import android.graphics.Color
import android.view.View
import androidx.fragment.app.Fragment
import cn.woochen.comlib.BaseFragment
import cn.woochen.comlib.util.OnSingleClickListener
import cn.woochen.comlib.util.ext.dp
import cn.woochen.comlib.util.ext.setOnSingleClickListener
import cn.yanhu.kuwanapp.R
import cn.yanhu.kuwanapp.databinding.FragmentEarnBinding
import cn.yanhu.kuwanapp.earn.activity.GameSearchActivity
import cn.yanhu.kuwanapp.home.MainViewModel
import cn.yanhu.kuwanapp.home.adapter.ViewPageAdapter
import cn.yanhu.kuwanapp.utils.helper.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView

/**
 *
 * 类描述：赚钱
 * @author：woochen
 * create at：2021/7/27 10:38 PM
 */
class EarnFragment : BaseFragment<FragmentEarnBinding, MainViewModel>(), OnSingleClickListener {

    override fun setContentView(): Int = R.layout.fragment_earn

    override fun initData() {
        initWidgets()
    }

    private fun initWidgets() {
        initTab()
    }

    override fun initListener() {
       mBinding.tvSearch.setOnSingleClickListener(this)
    }

    private val titles = mutableListOf("推荐", "快赚", "益智", "手游", "最新")
    private val fragments = mutableListOf<Fragment>()

    private fun initTab() {
        if (fragments.size > 0) return
        for (index in titles.indices) {
            fragments.add(EarnGameListFragment.newInstance(index))
        }
        val commonNavigator = CommonNavigator(mContext)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return titles.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val clipPagerTitleView = ClipPagerTitleView(context)
                clipPagerTitleView.text = titles[index]
                clipPagerTitleView.textSize = 14f.dp
                clipPagerTitleView.textColor = Color.parseColor("#666666")
                clipPagerTitleView.clipColor = Color.parseColor("#333333")
                clipPagerTitleView.setOnClickListener { mBinding.viewpager.currentItem = index }
                return clipPagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.roundRadius = 2f.dp
                indicator.lineHeight = 3f.dp
                indicator.xOffset = 13f.dp
                indicator.setColors(Color.parseColor("#FF6E28"))
                return indicator
            }
        }
        commonNavigator.isAdjustMode = false
        mBinding.magicIndicator.navigator = commonNavigator
        mBinding.viewpager.adapter = ViewPageAdapter(this, fragments)
        mBinding.viewpager.offscreenPageLimit = titles.size
        ViewPagerHelper.bind2(mBinding.magicIndicator, mBinding.viewpager)
    }

    override fun requestData() {

    }

    fun refreshData() {

    }

    companion object {
        fun newInstance() = EarnFragment()
    }

    override fun onSingleClick(v: View?) {
        when (v) {
            mBinding.tvSearch -> {//搜索
                GameSearchActivity.start(mContext)
            }
        }
    }
}